package com.eric.windows.actionbar;

import android.app.ActionBar;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.app.Activity;
import android.widget.FrameLayout;

import com.eric.windows.R;

public class ActionTabsActivity extends Activity implements ActionBar.TabListener {

    private FrameLayout frameActionContainer;
    private ActionBar actionBar;
    private Fragment fragment1;
    private Fragment fragment2;
    private Fragment fragment3;
    private FragmentTransaction fragmentTransaction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_action_tabs);
        frameActionContainer = (FrameLayout) findViewById(R.id.frame_action_container);
        actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        //创建fragments 要放置到newTab之前，否则会报 fragment 为null 异常
        fragment1 = new ActionFragment();
        fragment2 = new ActionFragment2();
        fragment3 = new ActionFragment3();

        ActionBar.Tab tab = actionBar.newTab()
                .setText("标题1")
                .setIcon(R.mipmap.ic_launcher)
                .setTabListener(this)
                .setTag(1);

        ActionBar.Tab tab2 = actionBar.newTab()
                .setIcon(R.mipmap.ic_launcher)
                .setText("标题2")
                .setTabListener(this)
                .setTag(2);

        ActionBar.Tab tab3 = actionBar.newTab()
                .setText("标题3")
                .setIcon(R.mipmap.ic_launcher)
                .setTabListener(this)
                .setTag(3);

        actionBar.addTab(tab);
        actionBar.addTab(tab2);
        actionBar.addTab(tab3);
        actionBar.setSelectedNavigationItem(1);//设置第一个默认的item

    }

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
        switch ((Integer)tab.getTag()){
            case 1:
                ft.replace(R.id.frame_action_container,fragment1);
                break;
            case 2:
                ft.replace(R.id.frame_action_container,fragment2);
                break;
            case 3:
                ft.replace(R.id.frame_action_container,fragment3);
                break;
        }
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
        switch ((Integer)tab.getTag()){
            case 1:
                ft.remove(fragment1);
                break;
            case 2:
                ft.remove(fragment2);
                break;
            case 3:
                ft.remove(fragment3);
                break;
        }
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {

    }




}
